package com.qyhl.webtv.module_news.news.catchnews;


import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.intergral.CoinBean;
import com.qyhl.webtv.commonlib.entity.news.CatchSmallBean;
import com.qyhl.webtv.commonlib.service.IntergralService;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.util.List;

/**
 * Created by qyhl on 2017/11/30.
 */

public class CatchNewsModel implements CatchNewsContract.CatchNewsModel {
    private CatchNewsPresenter mPresenter;


    @Autowired(name = ServicePathConstant.INTERGRAL_SERICE)
    public IntergralService intergralService;

    public CatchNewsModel(CatchNewsPresenter mPresenter) {
        this.mPresenter = mPresenter;
        ARouter.getInstance().inject(this);
    }



    /**
     * 获取抓取详情
     *
     * @param newsId
     */
    @Override
    public void getDetail(String newsId) {


        EasyHttp.get(NewsUrl.GET_VIDEO_COMMENT_DETAIL)
                .params("newsId", newsId)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", CommonUtils.getInstance().getSite()+ "")
                .execute(new SimpleCallBack<CatchSmallBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError(2, "暂无任何内容！");
                        } else {
                            mPresenter.setError(1, "解析出错，加载失败！");
                        }
                    }

                    @Override
                    public void onSuccess(CatchSmallBean catchSmallBean) {
                        mPresenter.setDetail(catchSmallBean);
                    }
                });

    }

    /**
     * 获取评论列表
     *
     * @param page
     * @param number
     * @param newsId
     */
    @Override
    public void getComment(final String page, String number, String newsId) {

        EasyHttp.get(NewsUrl.GET_COMMENT)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("page", page)
                .params("number", number)
                .params("newsId", newsId)
                .execute(new SimpleCallBack<List<CatchSmallBean.Comments>>() {

                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            if ("1".equals(page)) {
                                mPresenter.setCommentEmpty("暂无任何评论！");
                            } else {
                                mPresenter.setLog("no more");
                            }
                        } else {
                            if ("1".equals(page)) {
                                mPresenter.setCommentError("解析出错，加载失败！");
                            } else {
                                mPresenter.setCommentMessage("解析出错，加载失败！");
                            }
                        }
                    }

                    @Override
                    public void onSuccess(List<CatchSmallBean.Comments> comments) {
                        if (comments != null && comments.size() > 0) {
                            if ("1".equals(page)) {
                                mPresenter.setComment(comments, false);
                            } else {
                                mPresenter.setComment(comments, true);
                            }
                        } else {
                            if ("1".equals(page)) {
                                mPresenter.setCommentEmpty("暂无任何评论！");
                            } else {
                                mPresenter.setLog("no more");
                            }
                        }
                    }
                });


    }

    /**
     * 发送评论
     *
     * @param newsId
     * @param content
     * @param userName
     * @param nickName
     * @param userAvatar
     */
    @Override
    public void sendComment(String newsId, String content, String userName, String nickName, String userAvatar) {
        EasyHttp.post(NewsUrl.SEND_COMMENT)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("newId", newsId)
                .params("content", content)
                .params("userName", userName)
                .params("nickName", nickName)
                .params("userAvatar", userAvatar)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.sendError("网络异常，评论失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult= new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.sendSuccess("评论成功！");
                        } else {
                            mPresenter.sendError("解析出错，评论失败！");
                        }
                    }
                });

    }

    /**
     * 点击统计
     *
     * @param newsId
     */
    @Override
    public void count(String newsId) {

        EasyHttp.get(NewsUrl.GET_CATCH_NEWS_COUNT)
                .params("siteId", CommonUtils.getInstance().getSite()+ "")
                .params("newsId", newsId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {

                    }
                });


    }

    @Override
    public void getCoin(String type,String name,int ty) {

        intergralService.gainCoin(type,"name", new IntergralService.GainCoinCallBack() {
            @Override
            public void success(CoinBean coinBean) {
                mPresenter.getCoinSuccess(coinBean);
            }

            @Override
            public void error(String msg) {
                mPresenter.getCoinFailure(msg);
            }
        });



    }
}
